package cbs.jdtag.actions;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

import cbs.jdtag.AppContext;

public class ShowCommandHelpAction extends AbstractAction {

	private static final long serialVersionUID = -5874189792589757051L;

	JDialog helpDialog = new JDialog(AppContext.mainView, "Command help", false);
	JTextArea helpText = new JTextArea("\r\n" +
			"* Adding an alignment:\r\n\r\n" +
			"To add an alignment type the desired source nodes " +
			"and target nodes separated by a space in the input " +
			"field at the bottom of the application and press " +
			"<ENTER>. For instance, typing 'a1a5 b3b4b10' (without " +
			"the quotes) will align the source nodes a1 and a5 " +
			"with the target nodes b3, b4 and b10." +
			"\r\n\r\n\r\n" +
			"* Removing an alignment:\r\n\r\n" +
			"Removing an alignment is quite the same as adding " +
			"one: type the desired source nodes " +
			"and target nodes separated by a space in the input " +
			"field at the bottom of the application and press " +
			"<ENTER>. For instance, typing 'a1a5 b3b4b10' (without " +
			"the quotes) will remove the alignment between those " +
			"nodes." +
			"\r\n\r\n\r\n" +
			"* History:\r\n\r\n" +
			"The input field has a history. To get the previous " +
			"entered command(s), just type <UP ARROW> within the input " +
			"field. This history is is only avaible in the current session " +
			"and not saved (like it is done in the Bash from Linux)." + 
			"\r\n\r\n\r\n" +
			"* CAUTION:\r\n\r\n" +
			"If you, for instance, aligned the source nodes " +
			"a0 and a1 with the target node b2. Then it is only possible " +
			"to delete this alignment completely. You can NOT only " +
			"remove either a0 or a1 from the alignment." +
			"\r\n\r\n\r\n");
	
	public ShowCommandHelpAction() {
		super("Commands");
		
		putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK));
		
		helpText.setEditable(false);
		helpText.setLineWrap(true);
		helpText.setWrapStyleWord(true);
		
		helpDialog.setIconImage(AppContext.getIcon());
		helpDialog.setLayout(new BorderLayout());
		helpDialog.add(new JScrollPane(helpText,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
	}
	
	public void actionPerformed(ActionEvent e) {
		if (helpDialog.isVisible()) {
			helpDialog.toFront();
			return;
		}
		else {
			helpDialog.setSize(300, 300);
			helpDialog.setLocationRelativeTo(null);
			helpDialog.setVisible(true);
			helpDialog.toFront();
		}
	}
}
